package com.gzhryc.shared_device.oem.h5.mng.controllers.market.account;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.net_marketing.cloud.account.dao.db.CloudPayAccountWithdrawPayee;
import com.gzhryc.net_marketing.cloud.account.services.CloudPayAccountWithdrawPayeeService;
import com.gzhryc.net_marketing.cloud.account.services.dto.CloudPayAccountWithdrawPayeeSearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.enums.EAppType;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class CloudPayAccountWithdrawPayeeController {

    @Route(value = { "/account/withdrawPayee/list.html" })
    public String list(HttpServletRequest request) {
        return "/account/withdrawPayee/list.html";
    }

    @Route(value = { "/account/withdrawPayee/list.data" })
    public ListResult<CloudPayAccountWithdrawPayee> listData(HttpServletRequest request) {
        ListResult<CloudPayAccountWithdrawPayee> result = new ListResult<CloudPayAccountWithdrawPayee>(DtoConstants.NULL_DATA, "暂无数据");
        CloudPayAccountWithdrawPayeeSearch search = WebUtils.getParameter(request, CloudPayAccountWithdrawPayeeSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(CloudPayAccountWithdrawPayeeService.self().countJoinBySearch(search));
        result.getBody().setData(CloudPayAccountWithdrawPayeeService.self().findJoinBySearch(search, pageNo, pageLimit));

        if (result.getBody().getData() != null) {
            result.setCode(DtoConstants.SUCCESS, null);
        }
        return result;
    }

    @Route(value = { "/account/withdrawPayee/add.html" })
    public String info(HttpServletRequest request) {
        request.setAttribute("typeList", EAppType.values());
        return "/account/withdrawPayee/edit.html";
    }

    @Route(value = { "/account/withdrawPayee/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        CloudPayAccountWithdrawPayee withdrawPayee = WebUtils.getParameter(request,CloudPayAccountWithdrawPayee.class);
        try {
            if (CloudPayAccountWithdrawPayeeService.self().add(withdrawPayee)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/account/withdrawPayee/del.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        Long id = WebUtils.getLong(request,"id");
        if(id != null) {
            try {
                if (CloudPayAccountWithdrawPayeeService.self().deleteById(id)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
}
